/*
 * DxfHeader.java
 *
 * Created on 4 de Dezembro de 2007, 10:58
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package importadorDXF;

import mathematics.Point3D;

/**
 *
 * @author rafaelbuck
 */
public class DxfHeader extends DxfSection
{

    public void read(DxfGroups grp, DxfReporter reporter)
        throws DxfException
    {
        do
        {
            grp.read();
            switch(grp.number)
            {
            case 0: // '\0'
                if("ENDSEC".equals(grp.valToString()))
                    return;
                else
                    throw new DxfException("err!InHeader", new String[] {
                        String.valueOf(grp.getLineNr())
                    });

            case 9: // '\t'
                lastKeyword = grp.valToString();
                break;

            case 1: // '\001'
                if(lastKeyword.equals("$ACADVER"))
                {
                    ACADVER = grp.valToString();
                    reporter.showStatus("   $ACADVER=" + ACADVER);
                    String acadver = ACADVER.toUpperCase();
                    if(acadver != null)
                    {
                        String acadRelease = "???";
                        boolean warn = false;
                        if(acadver.startsWith("AC"))
                            try
                            {
                                String version = acadver.substring(2);
                                int dxfversion = Integer.parseInt(version);
                                if(dxfversion > 1014)
                                {
                                    acadRelease = "> R14";
                                    warn = true;
                                } else
                                {
                                    switch(dxfversion)
                                    {
                                    case 1014: 
                                        acadRelease = "R14";
                                        warn = true;
                                        break;

                                    case 1013: 
                                        acadRelease = "AutoDesk internal (>R13)";
                                        warn = true;
                                        break;

                                    case 1012: 
                                        acadRelease = "R13";
                                        warn = true;
                                        break;

                                    case 1010: 
                                    case 1011: 
                                        warn = true;
                                        acadRelease = "AutoDesk internal (>R12)";
                                        break;

                                    case 1009: 
                                        acadRelease = "R11/R12";
                                        break;

                                    case 1007: 
                                    case 1008: 
                                        acadRelease = "AutoDesk internal (>R10)";
                                        break;

                                    case 1006: 
                                        acadRelease = "R10";
                                        break;

                                    case 1005: 
                                        acadRelease = "AutoDesk internal (>R9)";
                                        break;

                                    case 1004: 
                                        acadRelease = "R9";
                                        break;

                                    case 1003: 
                                        acadRelease = "R8/V2.6";
                                        break;

                                    case 1002: 
                                        acadRelease = "R7/V2.5";
                                        // fall through

                                    default:
                                        if("2.10".equals(version))
                                        {
                                            acadRelease = "R6/V2.1";
                                            break;
                                        }
                                        if("1.50".equals(version))
                                            acadRelease = "R5/V2.0";
                                        break;
                                    }
                                }
                            }
                            catch(NumberFormatException _ex) { }
                        if(warn)
                            reporter.showWarningRes("warnAcadReleaseNew", new String[] {
                                acadRelease
                            });
                        else
                            reporter.showStatusRes("msgAcadRelease", new String[] {
                                acadRelease
                            });
                    }
                }
                break;

            case 3: // '\003'
                if(lastKeyword.equals("$DWGCODEPAGE"))
                {
                    DWGCODEPAGE = grp.valToString();
                    reporter.showStatus("   $DWGCODEPAGE=" + DWGCODEPAGE);
                }
                break;

            case 10: // '\n'
                if(lastKeyword.equals("$EXTMAX"))
                    EXTMAX.x = grp.valToFloat();
                else
                if(lastKeyword.equals("$EXTMIN"))
                    EXTMIN.x = grp.valToFloat();
                else
                if(lastKeyword.equals("$INSBASE"))
                    INSBASE.x = grp.valToFloat();
                break;

            case 20: // '\024'
                if(lastKeyword.equals("$EXTMAX"))
                    EXTMAX.y = grp.valToFloat();
                else
                if(lastKeyword.equals("$EXTMIN"))
                    EXTMIN.y = grp.valToFloat();
                else
                if(lastKeyword.equals("$INSBASE"))
                    INSBASE.y = grp.valToFloat();
                break;

            case 30: // '\036'
                if(lastKeyword.equals("$EXTMAX"))
                    EXTMAX.z = grp.valToFloat();
                else
                if(lastKeyword.equals("$EXTMIN"))
                    EXTMIN.z = grp.valToFloat();
                else
                if(lastKeyword.equals("$INSBASE"))
                    INSBASE.z = grp.valToFloat();
                break;

            case 40: // '('
                if(lastKeyword.equals("$LTSCALE"))
                    LTSCALE = grp.valToFloat();
                else
                if(lastKeyword.equals("$PDSIZE"))
                    PDSIZE = grp.valToFloat();
                break;

            case 70: // 'F'
                if(lastKeyword.equals("$FILLMODE"))
                    FILLMODE = grp.valToInt();
                else
                if(lastKeyword.equals("$PDMODE"))
                    PDMODE = grp.valToInt();
                break;
            }
        } while(true);
    }

    public int getCodePage()
    {
        if(codePage == -1)
            if(DWGCODEPAGE.equals("DOS850"))
                codePage = 3;
            else
            if(DWGCODEPAGE.equals("DOS852"))
                codePage = 4;
            else
            if(DWGCODEPAGE.equals("DOS855"))
                codePage = 5;
            else
            if(DWGCODEPAGE.equals("DOS857"))
                codePage = 6;
            else
            if(DWGCODEPAGE.equals("DOS860"))
                codePage = 7;
            else
            if(DWGCODEPAGE.equals("DOS861"))
                codePage = 8;
            else
            if(DWGCODEPAGE.equals("DOS863"))
                codePage = 9;
            else
            if(DWGCODEPAGE.equals("DOS864"))
                codePage = 10;
            else
            if(DWGCODEPAGE.equals("DOS865"))
                codePage = 11;
            else
            if(DWGCODEPAGE.equals("DOS869"))
                codePage = 12;
            else
            if(DWGCODEPAGE.equals("ISO8859_1"))
                codePage = 0;
            else
            if(DWGCODEPAGE.equals("ISO8859_2"))
                codePage = 1;
            else
            if(DWGCODEPAGE.equals("MAC_ROMAN"))
                codePage = 13;
            else
                codePage = 2;
        return codePage;
    }

    public String getACADVER()
    {
        return ACADVER;
    }

    public int getATTMODE()
    {
        return ATTMODE;
    }

    public String getDWGCODEPAGE()
    {
        return DWGCODEPAGE;
    }

    public Point3D getEXTMAX()
    {
        return EXTMAX;
    }

    public Point3D getEXTMIN()
    {
        return EXTMIN;
    }

    public boolean getFILLMODE()
    {
        return FILLMODE != 0;
    }

    public Point3D getINSBASE()
    {
        return INSBASE;
    }

    public int getPDMODE()
    {
        return PDMODE;
    }

    public float getPDSIZE()
    {
        return PDSIZE;
    }

    public float getLTSCALE()
    {
        return LTSCALE;
    }

    /** Creates a new instance of DxfHeader */
    public DxfHeader()
    {
        ATTMODE = 1;
        DWGCODEPAGE = "DOS437";
        EXTMAX = new Point3D();
        EXTMIN = new Point3D();
        INSBASE = new Point3D();
        LTSCALE = 1.0F;
        lastKeyword = "";
        codePage = -1;
    }

    public static final int UC_ISO_1 = 0;
    public static final int UC_ISO_2 = 1;
    public static final int UC_DOS437 = 2;
    public static final int UC_DOS850 = 3;
    public static final int UC_DOS852 = 4;
    public static final int UC_DOS855 = 5;
    public static final int UC_DOS857 = 6;
    public static final int UC_DOS860 = 7;
    public static final int UC_DOS861 = 8;
    public static final int UC_DOS863 = 9;
    public static final int UC_DOS864 = 10;
    public static final int UC_DOS865 = 11;
    public static final int UC_DOS869 = 12;
    public static final int UC_MAC = 13;
    public static final int UC_ALL = 14;
    protected String ACADVER;
    protected int ATTMODE;
    protected String DWGCODEPAGE;
    protected Point3D EXTMAX;
    protected Point3D EXTMIN;
    protected int FILLMODE;
    protected Point3D INSBASE;
    protected float LTSCALE;
    protected int PDMODE;
    protected float PDSIZE;
    protected String lastKeyword;
    protected int codePage;
}